<?php

namespace App\Services\Member;

use App\Models\Member\Member;
use App\Models\Member\WalletDetail;
use App\Models\Order\OrdersWallets;
use Illuminate\Support\Facades\DB;

class MoneyService
{
    /**
     * 收入
     * @param string $member_uuid  用户uuid
     * @param float $cash  单位元
     * @param int $source_type  数据来源类型 1充值 2提现 3广告收入 4打赏收入 5打赏支出 6活动收入 7活动支出 8余额提现退回(审核不通过) 200其他
     * @param string $content  具体变动内容说明
     * @return bool  返回值 true成功 false失败
     */
    public static function income(array $order_ids, string $member_uuid, float $cash, int $source_type, string $content='', string $img=''){
        try {
            DB::beginTransaction();

            $member = Member::where('uuid', $member_uuid)->first();
            $before_money = $member->money;

            //变动用户金额
            $after_money = bcadd($member->money, $cash, 2);
            $member->update([
                'money' => $after_money
            ]);

            switch ($source_type){
                case 1:  //充值
                    //增加可用总收入
                    $after_total_money = bcadd($member->total_money, $cash, 2);
                    $member->update([
                        'total_money' => $after_total_money
                    ]);
                    break;
            }

            //日志
            $walletDetail = WalletDetail::create([
                'member_uuid' => $member_uuid,
                'content' => $content,
                'before_money' => $before_money,
                'after_money' => $after_money,
                'cash' => $cash,
                'type' => 1,
                'month' => date('Y-m-01'),
                'source_type' => $source_type,
                'img' => $img,
            ]);

            foreach ($order_ids as $order_id){
                OrdersWallets::create([
                    'wallet_detail_id' => $walletDetail->id,
                    'order_id' => $order_id,
                ]);
            }

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();

            return false;
        }

        return true;
    }

    /**
     * 支出
     * @param string $member_uuid  用户uuid
     * @param float $cash  单位元
     * @param int $source_type  数据来源类型 1充值 2提现 3广告收入 4打赏收入 5打赏支出 6活动收入 7活动支出 8余额提现退回(审核不通过) 200其他
     * @param string $content  具体变动内容说明
     * @param string $img  显示图片
     * @return bool  返回值 true成功 false失败
     */
    public static function out(string $member_uuid, float $cash, int $source_type, string $content='', string $img=''){
        try {
            if($cash <= 0) return true;
            DB::beginTransaction();
            $member = Member::where('uuid', $member_uuid)->first();
            $before_money = $member->money;

            if($before_money < $cash) throw new \Exception('钱包余额不足');

            //变动用户金额
            $after_money = bcsub($member->money, $cash, 2);
            $member->update([
                'money' => $after_money
            ]);

            //日志
            WalletDetail::create([
                'member_uuid' => $member_uuid,
                'content' => $content,
                'before_money' => $before_money,
                'after_money' => $after_money,
                'cash' => $cash,
                'type' => 2,
                'month' => date('Y-m-01'),
                'source_type' => $source_type,
                'img' => $img,
            ]);

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();

            throw new \Exception($e->getMessage());
//            return false;
        }

        return true;

    }

}
